package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.WcBasicInfo;
import com.ruoyi.system.dto.FirstAuditReportEx;
import com.ruoyi.system.service.IWcBasicInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FirstAuditReport;
import com.ruoyi.system.service.IFirstAuditReportService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-03-26
 */
@Controller
@RequestMapping("/system/firstAuditReport")
public class FirstAuditReportController extends BaseController
{
    private String prefix = "system/firstAuditReport";

    @Autowired
    private IFirstAuditReportService firstAuditReportService;

    @Autowired
    IWcBasicInfoService wcBasicInfoService;

    @RequiresPermissions("system:firstAuditReport:view")
    @GetMapping()
    public String firstAuditReport()
    {
        return prefix + "/firstAuditReport";
    }

    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:firstAuditReport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody FirstAuditReport firstAuditReport)
    {
        startPage();
        List<FirstAuditReport> list = firstAuditReportService.selectFirstAuditReportList(firstAuditReport);
        List<FirstAuditReportEx> list2 = new ArrayList<>();
        list.stream().forEach(element -> {
            FirstAuditReportEx firstAuditReportEx = new FirstAuditReportEx();
            BeanUtils.copyProperties(element, firstAuditReportEx);
            if(element.getBaseinfoid() != null) {
                WcBasicInfo wcBasicInfo = wcBasicInfoService.selectWcBasicInfoById(element.getBaseinfoid());
                if (wcBasicInfo != null) {
                    BeanUtils.copyProperties(wcBasicInfo, firstAuditReportEx);
                }
            }

            list2.add(firstAuditReportEx);
        });

        return getDataTable(list2);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:firstAuditReport:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FirstAuditReport firstAuditReport)
    {
        List<FirstAuditReport> list = firstAuditReportService.selectFirstAuditReportList(firstAuditReport);
        ExcelUtil<FirstAuditReport> util = new ExcelUtil<FirstAuditReport>(FirstAuditReport.class);
        return util.exportExcel(list, "firstAuditReport");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:firstAuditReport:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FirstAuditReport firstAuditReport)
    {
        return toAjax(firstAuditReportService.insertFirstAuditReport(firstAuditReport));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FirstAuditReport firstAuditReport = firstAuditReportService.selectFirstAuditReportById(id);
        mmap.put("firstAuditReport", firstAuditReport);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:firstAuditReport:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FirstAuditReport firstAuditReport)
    {
        return toAjax(firstAuditReportService.updateFirstAuditReport(firstAuditReport));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:firstAuditReport:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(firstAuditReportService.deleteFirstAuditReportByIds(ids));
    }
}
